<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    :okButtonProps="{ props: {disabled: disableSubmit} }"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item label="客户类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" @change="typeChange"
                             v-decorator="['type', validatorRules.type, {'initialValue': 1}]"
                             :trigger-change="true"
                             dictCode="customer_type" placeholder="请选择客户类型" />

          <!--          <a-input-number v-decorator="['type', validatorRules.type]" placeholder="请输入客户类型：1个人客户，2企业客户" style="width: 100%"/>-->
          <!--          <a-select v-decorator="[ 'type', {}]" placeholder="请选择客户类型" :getPopupContainer= "(target) => target.parentNode">-->
          <!--            <a-select-option :value="1">个人客户</a-select-option>-->
          <!--            <a-select-option :value="2">企业客户</a-select-option>-->
          <!--          </a-select>-->
        </a-form-item>
        <a-form-item label="姓名/单位名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['name', validatorRules.name]" placeholder="请输入姓名/单位名称"></a-input>
        </a-form-item>
        <a-form-item label="联系地址/单位地址" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['contactAddress', validatorRules.contactAddress]" placeholder="请输入家庭住址/单位地址"></a-input>
        </a-form-item>
        <a-form-item label="身份证号/信用代码" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['code']" placeholder="请输入身份证号/信用代码"></a-input>
        </a-form-item>

        <a-form-item label="联系电话" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['phone', validatorRules.phone]" placeholder="请输入联系电话"></a-input>
        </a-form-item>

        <a-form-item v-show="customerType == 2" label="法人" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['legalPerson', validatorRules.legalPerson]" placeholder="请输入法人"></a-input>
        </a-form-item>
        <a-form-item v-show="customerType == 2" label="法人联系电话" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['legalPersonPhone', validatorRules.legalPersonPhone]"
                   placeholder="请输入法人联系电话"></a-input>
        </a-form-item>
        <a-form-item v-show="customerType == 2" label="法人身份证号码" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['legalPersonIdCard', validatorRules.legalPersonIdCard]"
                   placeholder="请输入法人身份证号码"></a-input>
        </a-form-item>

        <a-form-item label="家庭住址" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['homeAddress', validatorRules.homeAddress]" placeholder="请输入家庭住址"></a-input>
        </a-form-item>
        <a-form-item label="紧急联系人" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['emergencyContact', validatorRules.emergencyContact]" placeholder="请输入紧急联系人"></a-input>
        </a-form-item>
        <a-form-item label="紧急联系人关系" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list"
                             v-decorator="['emergencyContactRelation', validatorRules.emergencyContactRelation]"
                             :trigger-change="true"
                             dictCode="personnel_relationship" placeholder="请选择紧急联系人关系" />
        </a-form-item>
        <a-form-item label="紧急联系人电话" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['emergencyContactPhone', validatorRules.emergencyContactPhone]"
                   placeholder="请输入紧急联系人电话"></a-input>
        </a-form-item>
        <a-form-item label="邮箱地址" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['emailAddress', validatorRules.emailAddress]" placeholder="请输入邮箱地址"></a-input>
        </a-form-item>


        <a-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea v-decorator="['remark']" placeholder="请输入备注" rows="2"></a-textarea>
        </a-form-item>
      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>

import { httpAction } from '@/api/manage'
import pick from 'lodash.pick'
import { validateDuplicateValue } from '@/utils/util'
import JDate from '@/components/jeecg/JDate'
import JDictSelectTag from '@/components/dict/JDictSelectTag'
import idCard from '@/static/js/IdCard'

export default {
  name: 'CustomerInfoModal',
  components: {
    JDate,
    JDictSelectTag
  },
  data () {
    return {
      form: this.$form.createForm(this),
      title: '操作',
      width: 800,
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      disableSubmit: false,
      customerType: 1,
      validatorRules: {
        name: {
          rules: [
            { required: true, message: '请输入姓名/单位名称!' }
          ]
        },
        contactAddress: {
          rules: [
            { required: false, message: '请输入联系/单位地址!' }
          ]
        },
        code: {
          rules: [
            { required: true, message: '请输入身份证号/信用代码!' },
            // { pattern: /^[^_IOZSVa-z\W]{2}\d{6}[^_IOZSVa-z\W]{10}$/g, message: '请输入正确的手机号码!' },
            { validator: this.validateCode }
          ]
        },
        phone: {
          rules: [
            { required: true, message: '请输入联系电话!' },
            { pattern: /^((0\d{2,3}-\d{7,8})|(1[3456789]\d{9}))$/, message: '请输入正确的电话号码!' }
          ]
        },
        legalPerson: {
          rules: [
            { required: false, message: '请填写法人!' }
          ]
        },
        legalPersonPhone: {
          rules: [
            { required: false, message: '请填写法人联系电话!' },
            { pattern: /^((0\d{2,3}-\d{7,8})|(1[3456789]\d{9}))$/, message: '请输入正确的电话号码!' }
          ]
        },
        legalPersonIdCard: {
          rules: [
            { required: false, message: '请填写法人身份证号码!' },
            { validator: this.validateIdCode }
          ]
        },
        homeAddress: {
          rules: [
            { required: false, message: '请填写家庭住址!' }
          ]
        },
        emergencyContact: {
          rules: [
            { required: false, message: '请填写紧急联系人!' }
          ]
        },
        emergencyContactRelation: {
          rules: [
            { required: false, message: '请选择紧急联系人关系!' }
          ]
        },
        emergencyContactPhone: {
          rules: [
            { required: false, message: '请输入紧急联系人电话!' },
            { pattern: /^((0\d{2,3}-\d{7,8})|(1[3456789]\d{9}))$/, message: '请输入正确的电话号码!' }
          ]
        },
        emailAddress: {
          rules: [
            { required: false, type: 'email', message: '请输入邮箱地址!' }
          ]
        },
        type: {
          rules: [
            { required: true, message: '请选择客户类型!' }
          ]
        }
      },
      url: {
        add: '/zcgl/customerInfo/add',
        edit: '/zcgl/customerInfo/edit'
      }
    }
  },
  created () {
  },
  methods: {
    validateCode (rule, value, callback) {
      let pattern = /^[^_IOZSVa-z\W]{2}\d{6}[^_IOZSVa-z\W]{10}$/g
      if (value){
        if (!pattern.test(value) && !idCard.cnid(value)) {
          callback('请输入正确的身份证号或信用代码!')
        } else {
          callback()
        }
      } else {
        callback()
        // callback('请输入身份证号/信用代码!')
      }
    },
    validateIdCode (rule, value, callback) {
      if (value){
        if (!idCard.cnid(value)) {
          callback('请输入正确的身份证号!')
        } else {
          callback()
        }
      } else {
        // callback('请输入身份证号!')
        callback()
      }
    },
    add () {
      this.edit({type: 1})
    },
    typeChange (val) {
      if (val && val == 2) {
        this.customerType = 2
        // this.validatorRules.legalPerson.rules[0].required = true
        // this.validatorRules.legalPersonPhone.rules[0].required = true
        // this.validatorRules.legalPersonIdCard.rules[0].required = true
      } else {
        this.customerType = 1
        // this.validatorRules.legalPerson.rules[0].required = false
        // this.validatorRules.legalPersonPhone.rules[0].required = false
        // this.validatorRules.legalPersonIdCard.rules[0].required = false
      }
    },
    edit (record) {
      this.form.resetFields()
      this.model = Object.assign({}, record)
      if (!this.model.type) {
        this.model.type = 1
      }
      this.typeChange(this.model.type)
      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.model, 'name', 'contactAddress', 'code', 'phone', 'emailAddress', 'legalPerson', 'emergencyContactPhone', 'emergencyContactRelation', 'emergencyContact', 'legalPersonPhone', 'legalPersonIdCard', 'homeAddress', 'type', 'remark', 'updateBy', 'updateTime', 'createBy', 'createTime'))
      })
    },
    close () {
      this.$emit('close')
      this.visible = false
    },
    handleOk () {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          let formData = Object.assign(this.model, values)
          if (this.model.type == 1) {
            if (!idCard.cnid(this.model.code)) {
              that.$message.warning('请输入正确的身份证号码！')
              return
            }
          } else {
            let pattern = /^[^_IOZSVa-z\W]{2}\d{6}[^_IOZSVa-z\W]{10}$/g
            if (!pattern.test(this.model.code)) {
              that.$message.warning('请输入正确的信用代码！')
              return
            }
          }
          console.log('表单提交数据', formData)
          that.confirmLoading = true
          httpAction(httpurl, formData, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
              that.close()
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
          })
        }

      })
    },
    handleCancel () {
      this.close()
    },
    popupCallback (row) {
      this.form.setFieldsValue(pick(row, 'name', 'contactAddress', 'code', 'phone', 'emailAddress', 'legalPerson', 'emergencyContactPhone', 'emergencyContactRelation', 'emergencyContact', 'legalPersonPhone', 'legalPersonIdCard', 'homeAddress', 'type', 'remark', 'updateBy', 'updateTime', 'createBy', 'createTime'))
    }

  }
}
</script>